﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Portal.Nhibernate.Repository;

namespace Portal.Web.Areas.Admin.Controllers
{
    public class OrderController : BaseAdminController
    {
        private readonly IOrderRepository<IOrder> _orderRepository;

        public OrderController(IOrderRepository<IOrder> orderRepository)
        {
            _orderRepository = orderRepository;
        }

        public OrderController()
        {
            _orderRepository = new OrderRepository();
        }

        /// <summary>
        /// 订单管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {

            return View();
        }
        /// <summary>
        /// 获取订单列表JSON
        /// </summary>
        /// <param name="sort"></param>
        /// <param name="order"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        public ActionResult GetListJson(string sort = "Sort", string order = "DESC", int page = 1, int rows = 10)
        {
            return Json(_orderRepository.Get().Sorting(sort, order).JqEasyuiDataGrid(page, rows));
        }
     
        /// <summary>
        /// 新建订单
        /// </summary>
        /// <returns></returns>
        public ActionResult Create()
        {

            var model = _orderRepository.CreateEntity();
            if (Request.IsAjaxRequest())
                return View("CreateControl", model);
            return View(model);
        }
     
        /// <summary>
        /// 新建保存订单
        /// </summary>
        /// <param name="formCollection"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection formCollection, Link entity)
        {
            var model = _orderRepository.CreateEntity();
            model.Culture = Culture;
            model.Founder = CurrentUserName;
            _orderRepository.Add(model);
            _orderRepository.Save();
            if (Request.IsAjaxRequest())
                return Json(new AjaxResult { message = "添加成功", state = true });
            ViewData["Result"] = new AjaxResult { message = "添加成功", state = true };
            return View(model);
        }
        /// <summary>
        /// 编辑订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Edit(string id)
        {
            var model = _orderRepository.Get().SingleOrDefault(o => o.Id == id);
            if (model == null)
                return NotFount(new AjaxResult { message = "没有找到记录", state = false });
            if (Request.IsAjaxRequest())
                return View("EditControl", model);
            return View(model);
        }
       
        /// <summary>
        /// 编辑保存订单
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="linkImage"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(Link entity, HttpPostedFileBase linkImage)
        {
            var model = _orderRepository.Get().SingleOrDefault(o => o.Id == entity.Id);
            if (model == null)
                return NotFount(new AjaxResult { message = "没有找到记录", state = false });
            model.Culture = Culture;
            model.Founder = CurrentUserName;
            _orderRepository.Update(model);
            _orderRepository.Save();
            if (Request.IsAjaxRequest())
                return Json(new AjaxResult { message = "修改成功", state = true });
            ViewData["Result"] = new AjaxResult { message = "修改成功", state = true };
            return View(model);
        }
     
        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(List<string> id, FormCollection collection)
        {
            var list = _orderRepository.Get(o => id.Contains(o.Id));
            list.ForEach(o => _orderRepository.Delete(o));
            _orderRepository.Save();

            return Json(new AjaxResult { state = true, message = "删除成功" });
        }

    }
}
